﻿
@{
    ViewBag.Title = "Form";
    Layout = "~/Views/Shared/_Form.cshtml";
}
<style>
    .form .formValue input.form-control {
        height: 34px;
        line-height: 34px;
    }

    .form-control {
        padding: 0px;
    }

    .autocomplete-suggestions {
        -webkit-box-sizing: border-box;
        -moz-box-sizing: border-box;
        box-sizing: border-box;
        border: 1px solid #999;
        background: #FFF;
        cursor: default;
        overflow: auto;
        -webkit-box-shadow: 1px 4px 3px rgba(50, 50, 50, 0.64);
        -moz-box-shadow: 1px 4px 3px rgba(50, 50, 50, 0.64);
        box-shadow: 1px 4px 3px rgba(50, 50, 50, 0.64);
    }

    .autocomplete-selected {
        background: #F0F0F0;
    }

    .autocomplete-suggestion {
        padding: 2px 5px;
        white-space: nowrap;
        overflow: hidden;
    }

    .autocomplete-no-suggestion {
        padding: 2px 5px;
    }

    ul {
        list-style: none;
        float: left;
        -webkit-padding-start: 10px;
    }

        ul li {
            list-style-type: none;
            float: left;
        }

    .active {
        border: 1px solid #1ABC9C;
    }

        .active:hover {
            background-color: #1ABC9C;
            color: #fff;
            text-decoration: none;
        }

    a {
        padding: 3px 10px;
    }

        a:hover {
            border: 1px solid #1ABC9C;
            text-decoration: none;
        }
</style>
<script>
    var F_ClientType = "";
    var F_ClientId = "";
    var resultData = {};
    $(function () { 
        F_ClientType = $.request("F_ClientType");
        F_ClientId = $.request("F_ClientId");
        $("#F_ActivityCityId").select2({ multiple: true });
        GetInit();
    });
    function GetInit() {
        resultData = getCache("CLIENT_ADDPROJECT");
        if (!resultData || resultData.length <= 0) {
            $.ajax({
                url: "/ClientManager/ClientInfos/GetItemListJson",
                dataType: "json",
                async: false,
                success: function (data) {
                    resultData = data;
                    InitControl();
                    setCache("CLIENT_ADDPROJECT", data);
                }, error: function (XMLHttpRequest, textStatus, errorThrown) {
                }
            });
        } else {
            InitControl();
        }
    }

    function InitControl() {        
        var F_DepartmentId = resultData.F_DepartmentId;
        for (var i = 0; i < F_DepartmentId.length; i++) {
            $("#F_DepartmentId").append("<option value='" + F_DepartmentId[i].F_ItemCode + "'>" + F_DepartmentId[i].F_ItemName + "</option>");
        }

        var F_PositionId = resultData.F_PositionId;
        for (var i = 0; i < F_PositionId.length; i++) {
            $("#F_PositionId").append("<option value='" + F_PositionId[i].F_ItemCode + "'>" + F_PositionId[i].F_ItemName + "</option>");
        }

        var F_IndustryId = resultData.F_IndustryId;
        for (var i = 0; i < F_IndustryId.length; i++) {
            $("#F_ZkIndustryId").append("<option value='" + F_IndustryId[i].F_ItemCode + "'>" + F_IndustryId[i].F_ItemName + "</option>");
        }

        var F_ActivityType = resultData.F_ActivityType;
        var First_ActivityType = resultData.First_ActivityType;
        var strHtml = "";
        for (var j = 0; j < First_ActivityType.length; j++) {
            strHtml += "<option style=\"font-weight:bold;\" label='" + First_ActivityType[j]["F_ItemName"] + "'>";
            for (var i = 0; i < F_ActivityType.length; i++) {
                if (First_ActivityType[j].F_ItemCode == F_ActivityType[i].F_Description) {
                    strHtml += "<option value='" + F_ActivityType[i].F_ItemCode + "'>" + "&nbsp;&nbsp;&nbsp;&nbsp;" + F_ActivityType[i].F_ItemName + "</option>";
                }
            }
            strHtml += "</option>"; 
        }
        $("#F_ActivityType").append(strHtml);

        //var F_ActivityCityId = resultData.F_CityId;
        //for (var i = 0; i < F_ActivityCityId.length; i++) {
        //    $("#F_ActivityCityId").append("<option value='" + F_ActivityCityId[i].F_EnCode + "'>" + F_ActivityCityId[i].F_FullName + "</option>");
        //}
        $("#F_ActivityCityId").bindSelect({
            url: "/ClientManager/ClientInfos/GetAllCitys",
            search: true,
            multiple: true,
            id: "F_EnCode",
            text: "F_FullName"
        });

        var F_SiteType = resultData.F_SiteType;
        for (var i = 0; i < F_SiteType.length; i++) {
            $("#F_SiteType").append("<option value='" + F_SiteType[i].F_ItemCode + "'>" + F_SiteType[i].F_ItemName + "</option>");
        }

        var F_CityId = resultData.F_CityId;
        for (var i = 0; i < F_CityId.length; i++) {
            $("#F_CityId").append("<option value='" + F_CityId[i].F_Id + "'>" + F_CityId[i].F_ItemName + "</option>");
        }

        if (F_ClientType == "2") {
            var Clients = [];
            var SiteClients = resultData.SiteClients;
            for (var i = 0; i < SiteClients.length; i++) {
                Clients.push({ value: SiteClients[i].F_ClientName, data: SiteClients[i].F_Id });
            }

            $("#F_MappingId").autocomplete({
                lookup: Clients,
                onSelect: function (suggestion) {
                    $("#hd_F_MappingId").val(suggestion.data);
                }
            });
        } else {
            $("#F_MappingId").attr("disabled","disabled");
            $("#F_MappingId").css("background-color", "#ddd");

            $("#F_ZkIndustryId").attr("disabled", "disabled");
            $("#F_ZkIndustryId").css("background-color", "#ddd");
        }
        ProjectChange(F_ClientId);
        ContactsChange(F_ClientId);
    }

    function ContactsChange(f_fartherId) {
        var contacts = [];
        var sitecontacts = resultData.Contacts;
        for (var i = 0; i < sitecontacts.length; i++) {
            if (sitecontacts[i].F_ClientId == f_fartherId) {
                contacts.push({ value: sitecontacts[i].F_Contact, data: sitecontacts[i].F_Id });
            }
        }
        $("#F_Contact").autocomplete({
            lookup: contacts,
            onSelect: function (suggestion) {
                $("#hd_F_Contact").val(suggestion.data);
                ContactInfo(suggestion.data);
            }
        });
    }

    function ContactInfo(contactId) {
        $.ajax({
            url: "/ClientManager/ClientInfos/GetContactInfo?F_ContactId=" + contactId,
            dataType: "json",
            async: false,
            success: function (data) {
                if (data != null) {
                    $("#F_DepartmentId").val(data["F_DepartmentId"]);
                    $("#F_PositionId").val(data["F_PositionId"]);
                    $("#F_Phone").val(data["F_Phone"]);
                    $("#F_Telephone").val(data["F_Telephone"]);
                    $("#F_Mail").val(data["F_Mail"]);
                    $("input[name='F_Sex'][value=" + data["F_Sex"] + "]").attr("checked", true);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) { }
        });
    }

    function ProjectChange(f_fartherId) {
        var Projects = [];
        var siteProjects = resultData.Projects;
        for (var i = 0; i < siteProjects.length; i++) {
            if (siteProjects[i].F_ClientId == f_fartherId) {
                Projects.push({ value: siteProjects[i].F_ProjectName, data: siteProjects[i].F_Id });
            }
        }

        $("#F_ProjectId").autocomplete({
            lookup: Projects,
            onSelect: function (suggestion) {
                $("#hd_F_ProjectId").val(suggestion.data);
                //ProjectInfo(suggestion.data);
            }
        });
    }

    var contracts;
    function ProjectInfo(projectId) {
        $.ajax({
            url: "/ClientManager/ClientInfos/GetProjectInfo?F_Id=" + projectId,
            dataType: "json",
            async: false,
            success: function (data) {
                contracts = data;
                if (data.length > 1) {
                    $("#contactArea").show();
                    $("#contactNum").html(data.length);
                    $("#contractList").empty();
                    for (var i = 0; i < data.length; i++) {
                        if (i == 0) {
                            $("#contractList").append("<li id=" + data[i]["F_Id"] + "><a style=\"cursor:pointer;\" onclick=\"ChangeContract(this)\" class=\"active\">" + data[i]["F_Contact"] + "</a></li>");
                            $("#F_Contact").val(data[i]["F_Contact"]);
                            $("#F_DepartmentId").val(data[i]["F_DepartmentId"]);
                            $("#F_PositionId").val(data[i]["F_PositionId"]);
                            $("#F_Phone").val(data[i]["F_Phone"]);
                            $("#F_Telephone").val(data[i]["F_Telephone"]);
                            $("#F_Mail").val(data[i]["F_Mail"]);
                            $("input[name='F_Sex'][value=" + data[i]["F_Sex"] + "]").attr("checked", true);
                        } else {
                            $("#contractList").append("<li id=" + data[i]["F_Id"] + "><a style=\"cursor:pointer;\" onclick=\"ChangeContract(this)\">" + data[i]["F_Contact"] + "</a></li>");
                        }
                    }
                } else {
                    $("#contactArea").hide();
                    $("#F_Contact").val(data[0]["F_Contact"]);
                    $("#F_DepartmentId").val(data[0]["F_DepartmentId"]);
                    $("#F_PositionId").val(data[0]["F_PositionId"]);
                    $("#F_Phone").val(data[0]["F_Phone"]);
                    $("#F_Telephone").val(data[0]["F_Telephone"]);
                    $("#F_Mail").val(data[0]["F_Mail"]);
                    $("input[name='F_Sex'][value=" + data[0]["F_Sex"] + "]").attr("checked", true);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) { }
        });
    }

    function ChangeContract(obj) {
        var data = $("#contractList").find("li");
        for (var i = 0; i < data.length; i++) {
            if (data[i].id == obj.parentNode.id) {
                data[i].childNodes[0].className = "active";
                for (var j = 0; j < contracts.length; j++) {
                    if (contracts[j]["F_Id"] == obj.parentNode.id) {
                        $("#F_Contact").val(contracts[j]["F_Contact"]);
                        $("#F_DepartmentId").val(contracts[j]["F_DepartmentId"]);
                        $("#F_PositionId").val(contracts[j]["F_PositionId"]);
                        $("#F_Phone").val(contracts[j]["F_Phone"]);
                        $("#F_Telephone").val(contracts[j]["F_Telephone"]);
                        $("#F_Mail").val(contracts[j]["F_Mail"]);
                        $("input[name='F_Sex'][value=" + contracts[j]["F_Sex"] + "]").attr("checked", true);
                    }
                }
            } else {
                data[i].childNodes[0].className = "";
            }
        }
    }

    function ChangeCityData() {
        var id = $("#F_CityId").val();
        $("#F_AreaId").empty();
        var F_AreaId = resultData.F_AreaId;
        for (var i = 0; i < F_AreaId.length; i++) {
            if (F_AreaId[i].F_ParentId == id) {
                $("#F_AreaId").append("<option value='" + F_AreaId[i].F_ItemCode + "'>" + F_AreaId[i].F_ItemName + "</option>");
            }
        }
    }

    var old_clientName;
    var old_brand;
    var old_project;
    var old_contact;
    function submitForm() {
        if (!$('#projectForm').formValid()) {
            return false;
        }
        var F_Source = $("#F_Source").val();
        var F_ActivityTheme = $("#F_ActivityTheme").val();
        var F_ActivityType = $("#F_ActivityType").val();
        var F_DemandCash = $("#F_DemandCash").val();

        var F_BuildDate = $("#F_BuildDate").val();
        var F_ActivityStartDate = $("#F_ActivityStartDate").val();
        var F_ActivityEndDate = $("#F_ActivityEndDate").val();
        var F_ACityIdList = $("#F_ActivityCityId").select2("data");
        var F_ActivityCityId = "";
        for (var i = 0; i < F_ACityIdList.length; i++) {
            if (i == F_ACityIdList.length - 1) {
                F_ActivityCityId += F_ACityIdList[i].id;
            } else {
                F_ActivityCityId += F_ACityIdList[i].id + ",";
            }
        }
        var F_SiteType = $("#F_SiteType").val();
        var F_SiteArea = $("#F_SiteArea").val();
        var F_PersionNumber = $("#F_PersionNumber").val();
        var F_Height = $("#F_Height").val();
        var F_Note = $("#F_Note").val();

        var hd_F_MappingId = $("#hd_F_MappingId").val();
        var txtF_MappingId = $("#F_MappingId").val();
        var F_MappingId = "";
        var F_ZkIndustryId = "";
        var F_ClientType2 = F_ClientType;
        if (F_ClientType2 == "1") {
            F_MappingId = F_ClientId;
        } else {
            F_MappingId = hd_F_MappingId == "" ? txtF_MappingId : hd_F_MappingId;
            F_ZkIndustryId = $("#F_ZkIndustryId").val();
        }

        var hd_F_BrandId = $("#hd_F_BrandId").val();
        var txtF_BrandId = $("#F_BrandId").val();
        var F_BrandId = hd_F_BrandId == "" ? txtF_BrandId : hd_F_BrandId;

        var hd_F_ProjectId = $("#hd_F_ProjectId").val();
        var txtF_ProjectId = $("#F_ProjectId").val();
        var F_ProjectId = hd_F_ProjectId == "" ? txtF_ProjectId : hd_F_ProjectId;

        var hd_F_Contact = $("#hd_F_Contact").val();
        var txtF_Contact = $("#F_Contact").val();
        var F_Contact = hd_F_Contact == "" ? txtF_Contact : hd_F_Contact;

        var F_DepartmentId = $("#F_DepartmentId").val();
        var F_PositionId = $("#F_PositionId").val();
        var F_Sex = $("input[name='F_Sex']:checked").val();
        var F_Phone = $("#F_Phone").val();
        var F_Telephone = $("#F_Telephone").val();
        var F_Mail = $("#F_Mail").val();   
        $.submitForm({
            url: "/ClientManager/ClientInfos/SubmitProjectData",
            param: {
                F_ClientId: F_ClientId, F_ClientType: F_ClientType,F_Source: F_Source, F_ActivityTheme: F_ActivityTheme, F_ActivityType: F_ActivityType, F_DemandCash: F_DemandCash,
                F_ActivityStartDate: F_ActivityStartDate, F_ActivityEndDate: F_ActivityEndDate, F_ActivityCityId: F_ActivityCityId, F_SiteType: F_SiteType, F_SiteArea: F_SiteArea,
                F_PersionNumber: F_PersionNumber, F_Height: F_Height, F_Note: F_Note, F_MappingId: F_MappingId, F_BrandId: F_BrandId,
                F_ProjectId: F_ProjectId, F_ContactId: F_Contact, F_DepartmentId: F_DepartmentId, F_PositionId: F_PositionId, F_Sex: F_Sex,
                F_Phone: F_Phone, F_Telephone: F_Telephone, F_Mail: F_Mail, F_BuildDate: F_BuildDate,
                F_ZkIndustryId: F_ZkIndustryId
            },
            success: function () {
                top.frames["editForm33"].$("#gridListqq").trigger("reloadGrid");
            }
        })
    }
</script>
<div id="ProjectsDetail">
    <form id="projectForm">
        <table class="form">
            <tr>
                <td colspan="6" class="interval">需求信息</td>
            </tr>
            <tr>
                <td colspan="6">
                    <table class="form">
                        <tr>
                            <td class="formTitle">需求来源</td>
                            <td class="formValue">
                                <select id="F_Source" name="F_Source" class="form-control required">
                                    <option value="">==请选择需求来源==</option>
                                    <option value="0">400电话</option>
                                    <option value="1">销售自拓</option>
                                    <option value="2">网站订单</option>
                                    <option value="3">小程序订单</option>
                                    <option value="4">App订单</option>
                                </select>
                            </td>
                            <td class="formTitle">所属行业</td>
                            <td class="formValue">
                                <select id="F_ZkIndustryId" name="F_ZkIndustryId" class="form-control">
                                    <option value="">==请选择行业==</option>
                                </select>
                            </td>
                            <td class="formTitle">活动主题</td>
                            <td class="formValue">
                                <input type="text" id="F_ActivityTheme" name="F_ActivityTheme" class="form-control required" placeholder="请输入活动主题" />
                            </td>                            
                        </tr>
                        <tr>         
                            <td class="formTitle">活动类型</td>
                            <td class="formValue">
                                <select id="F_ActivityType" name="F_ActivityType" class="form-control">
                                    <option value="">==请选择活动类型==</option>
                                </select>
                            </td>
                            <td class="formTitle">搭建日期</td>
                            <td class="formValue">
                                <input type="text" id="F_BuildDate" name="F_BuildDate" class="form-control input-wdatepicker" onfocus="WdatePicker()" placeholder="请输入活动搭建日期" />
                            </td>
                            <td class="formTitle">活动开始时间</td>
                            <td class="formValue">
                                <input type="text" id="F_ActivityStartDate" name="F_ActivityStartDate" class="form-control input-wdatepicker required" onfocus="WdatePicker()" placeholder="请输入活动开始时间" />
                            </td>                            
                        </tr>
                        <tr>
                            <td class="formTitle">活动结束时间</td>
                            <td class="formValue">
                                <input type="text" id="F_ActivityEndDate" name="F_ActivityEndDate" class="form-control input-wdatepicker required" onfocus="WdatePicker()" placeholder="请输入活动结束时间" />
                            </td>
                            <td class="formTitle">预算金额</td>
                            <td class="formValue">
                                <input type="text" id="F_DemandCash" name="F_DemandCash" class="form-control required number" placeholder="请输入预算金额" />
                            </td>
                            <td class="formTitle">活动城市</td>
                            <td class="formValue">
                                <select id="F_ActivityCityId" name="F_ActivityCityId" class="form-control required"></select>
                            </td>                                                       
                        </tr>
                        <tr>
                            <td class="formTitle">场地类型</td>
                            <td class="formValue">
                                <select id="F_SiteType" name="F_SiteType" class="form-control required">
                                    <option value="">==请选择场地类型==</option>
                                </select>
                            </td> 
                            <td class="formTitle">活动人数</td>
                            <td class="formValue">
                                <input type="text" id="F_PersionNumber" name="F_PersionNumber" class="form-control number" placeholder="请输入活动人数" />
                            </td>
                            <td class="formTitle">场地面积(㎡)</td>
                            <td class="formValue">
                                <input type="text" id="F_SiteArea" name="F_SiteArea" class="form-control number" placeholder="请输入场地面积" />
                            </td>                                                     
                        </tr>
                        <tr>
                            <td class="formTitle">层高(m)</td>
                            <td class="formValue">
                                <input type="text" id="F_Height" name="F_Height" class="form-control number" placeholder="请输入层高" />
                            </td>  
                            <td class="formTitle"></td>
                            <td class="formValue"></td> 
                            <td class="formTitle"></td>
                            <td class="formValue"></td>
                        </tr>
                        <tr>
                            <td class="formTitle">备注</td>
                            <td class="formValue" colspan="5">
                                <textarea rows="3" id="F_Note" name="F_Note" class="form-control" placeholder="请输入备注信息" style="width:99%;"></textarea>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr>
                <td colspan="4" class="interval">品牌项目组信息</td>
                <td colspan="2" class="interval"><div id="contactArea" style="display:none;float:left;height:35px;line-height:35px;width:100%;"><span style="float:left;">共<em id="contactNum" style="color:red;">0</em>个联系人</span><ul id="contractList"></ul></div></td>
            </tr>
            <tr>
                <td colspan="6">
                    <table class="form">
                        <tr>
                            <td class="formTitle">直客名称</td>
                            <td class="formValue">
                                <div class="input-group" style="width:100%;">
                                    <input type="text" id="F_MappingId" class="form-control" placeholder="请输入直客名称" />
                                    <span class="input-group-addon"><i class="glyphicon glyphicon-search"></i></span>
                                    <input type="hidden" name="hd_F_MappingId" id="hd_F_MappingId" value="" />
                                </div>
                            </td>                            
                            <td class="formTitle">品牌名称</td>
                            <td class="formValue">
                                <div class="input-group" style="width:100%;">
                                    <input type="text" id="F_BrandId" class="form-control" placeholder="请输入品牌名称" />
                                    <span class="input-group-addon"><i class="glyphicon glyphicon-search"></i></span>
                                    <input type="hidden" name="hd_F_BrandId" id="hd_F_BrandId" value="" />
                                </div>
                            </td>
                            <td class="formTitle">所属项目组</td>
                            <td class="formValue">
                                <div class="input-group" style="width:100%;">
                                    <input type="text" id="F_ProjectId" class="form-control" placeholder="请输入项目组" />
                                    <span class="input-group-addon"><i class="glyphicon glyphicon-search"></i></span>
                                    <input type="hidden" name="hd_F_ProjectId" id="hd_F_ProjectId" value="" />
                                </div>
                            </td>
                        </tr>
                        <tr>                            
                            <td class="formTitle">联系人</td>
                            <td class="formValue">
                                <div class="input-group" style="width:100%;">
                                    <input type="text" id="F_Contact" name="F_Contact" class="form-control required" placeholder="请输入联系人" />
                                    <span class="input-group-addon"><i class="glyphicon glyphicon-search"></i></span>
                                    <input type="hidden" name="hd_F_Contact" id="hd_F_Contact" value="" />
                                </div>
                            </td>
                            <td class="formTitle">所属部门</td>
                            <td class="formValue">
                                <select id="F_DepartmentId" name="F_DepartmentId" class="form-control">
                                    <option value="">==请选择部门==</option>
                                </select>
                            </td> 
                            <td class="formTitle">所属职位</td>
                            <td class="formValue">
                                <select id="F_PositionId" name="F_PositionId" class="form-control">
                                    <option value="">==请选择职位==</option>
                                </select>
                            </td>
                        </tr>
                        <tr>
                            
                            <td class="formTitle">性别</td>
                            <td class="formValue">
                                <input type="radio" name="F_Sex" value="1" />男<input type="radio" name="F_Sex" value="0" />女
                            </td>
                            <td class="formTitle">手机号码</td>
                            <td class="formValue">
                                <input type="text" id="F_Phone" name="F_Phone" class="form-control required isMobile" placeholder="请输入手机号" />
                            </td>
                            <td class="formTitle">公司座机</td>
                            <td class="formValue">
                                <input type="text" id="F_Telephone" name="F_Telephone" class="form-control isPhone" placeholder="请输入公司座机" />
                            </td>
                        </tr>
                        <tr>
                            
                            <td class="formTitle">Email</td>
                            <td class="formValue">
                                <input type="text" id="F_Mail" name="F_Mail" class="form-control email" placeholder="请输入Email" />
                            </td>
                            <td class="formTitle"></td>
                            <td class="formValue"></td>  
                            <td class="formTitle"></td>
                            <td class="formValue"></td>  
                        </tr>
                    </table>
                </td>
            </tr>              
        </table>
    </form>
</div>